/*
 * Copyright (C) 2004-2006  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
#include "stdafx.h"
#include "FdoWcsXmlGlobals.h"
#include <WCS/Override/FdoWcsOvBoundingBoxDefinition.h>

#define  MAX_DOUBLE_SIZE 64

FdoWcsOvBoundingBoxDefinition::FdoWcsOvBoundingBoxDefinition(void) :
    m_pXmlContentHandler(NULL),
    m_minX(0),
    m_minY(0),
    m_maxX(0),
    m_maxY(0)
{
}

FdoWcsOvBoundingBoxDefinition::~FdoWcsOvBoundingBoxDefinition(void)
{
    FDO_SAFE_RELEASE(m_pXmlContentHandler);
}

FdoWcsOvBoundingBoxDefinition* FdoWcsOvBoundingBoxDefinition::Create()
{
    return new FdoWcsOvBoundingBoxDefinition();
}

FdoString* FdoWcsOvBoundingBoxDefinition::GetCRS(void) const
{
    return m_crs;
}

void FdoWcsOvBoundingBoxDefinition::SetCRS(FdoString* crs)
{
    m_crs = crs;
}

FdoDouble FdoWcsOvBoundingBoxDefinition::GetMinX() const
{
    return m_minX;
}

void FdoWcsOvBoundingBoxDefinition::SetMinX(FdoDouble value)
{
    m_minX = value;
}

FdoDouble FdoWcsOvBoundingBoxDefinition::GetMinY() const
{
    return m_minY;
}

void FdoWcsOvBoundingBoxDefinition::SetMinY(FdoDouble value)
{
    m_minY = value;
}

FdoDouble FdoWcsOvBoundingBoxDefinition::GetMaxX() const
{
    return m_maxX;
}

void FdoWcsOvBoundingBoxDefinition::SetMaxX(FdoDouble value)
{
    m_maxX = value;
}

FdoDouble FdoWcsOvBoundingBoxDefinition::GetMaxY() const
{
    return m_maxY;
}

void FdoWcsOvBoundingBoxDefinition::SetMaxY(FdoDouble value)
{
    m_maxY = value;
}

void FdoWcsOvBoundingBoxDefinition::InitFromXml(FdoXmlSaxContext* context, FdoXmlAttributeCollection* attrs)
{
    //return BaseType::InitFromXml(pContext, attrs);
    try {
        VALIDATE_ARGUMENT(attrs);
        VALIDATE_ARGUMENT(context);

        FdoXmlAttributeP att = attrs->FindItem(FdoWcsXmlGlobals::g_WcsBoundingBoxCRS);
        if (att != NULL) {
            SetCRS(att->GetValue());//FdoCommonOSUtil::wtof
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }
}

FdoXmlSaxHandler* FdoWcsOvBoundingBoxDefinition::XmlStartElement(
    FdoXmlSaxContext* context, 
    FdoString* uri, 
    FdoString* name, 
    FdoString* qname, 
    FdoXmlAttributeCollection* atts)
{
    FdoXmlSaxHandler* pRet = NULL;

    try 
    {
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);

        pRet = BaseType::XmlStartElement(context, uri, name, qname, atts);
        if (pRet == NULL) {
            if (FdoCommonStringUtil::StringCompareNoCase(name, FdoWcsXmlGlobals::g_WcsBoundingBoxMinX) == 0 ||
                FdoCommonStringUtil::StringCompareNoCase(name, FdoWcsXmlGlobals::g_WcsBoundingBoxMinY) == 0 ||
                FdoCommonStringUtil::StringCompareNoCase(name, FdoWcsXmlGlobals::g_WcsBoundingBoxMaxX) == 0 ||
                FdoCommonStringUtil::StringCompareNoCase(name, FdoWcsXmlGlobals::g_WcsBoundingBoxMaxY) == 0) 
            {
                m_pXmlContentHandler = FdoXmlCharDataHandler::Create();
                pRet = m_pXmlContentHandler;
            }
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return pRet;
}

FdoBoolean FdoWcsOvBoundingBoxDefinition::XmlEndElement(FdoXmlSaxContext* context, FdoString* uri, FdoString* name, FdoString* qname)
{
    try {
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);

        BaseType::XmlEndElement(context, uri, name, qname);

        if (NULL != m_pXmlContentHandler)
        {
            if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::g_WcsBoundingBoxMinX) == 0) {
                SetMinX(FdoCommonOSUtil::wtof(m_pXmlContentHandler->GetString()));
            }
            else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::g_WcsBoundingBoxMinY) == 0) {
                SetMinY(FdoCommonOSUtil::wtof(m_pXmlContentHandler->GetString()));
            }
            else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::g_WcsBoundingBoxMaxX) == 0) {
                SetMaxX(FdoCommonOSUtil::wtof(m_pXmlContentHandler->GetString()));
            }
            else if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::g_WcsBoundingBoxMaxY) == 0) {
                SetMaxY(FdoCommonOSUtil::wtof(m_pXmlContentHandler->GetString()));
            }

            FDO_SAFE_RELEASE(m_pXmlContentHandler);
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return false;
}

void FdoWcsOvBoundingBoxDefinition::_writeXml( FdoXmlWriter* xmlWriter, const FdoXmlFlags* flags )
{
    VALIDATE_ARGUMENT(xmlWriter);
    VALIDATE_ARGUMENT(flags);

    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsBoundingBox);
    BaseType::_writeXml(xmlWriter, flags);

    xmlWriter->WriteAttribute(FdoWcsXmlGlobals::g_WcsBoundingBoxCRS, GetCRS());

    wchar_t pwc[MAX_DOUBLE_SIZE];

    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsBoundingBoxMinX);
    FdoCommonStringUtil::FormatDouble(GetMinX(), pwc, MAX_DOUBLE_SIZE);
    xmlWriter->WriteCharacters(pwc);
    xmlWriter->WriteEndElement();

    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsBoundingBoxMinY);
    FdoCommonStringUtil::FormatDouble(GetMinY(), pwc, MAX_DOUBLE_SIZE);
    xmlWriter->WriteCharacters(pwc);
    xmlWriter->WriteEndElement();

    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsBoundingBoxMaxX);
    FdoCommonStringUtil::FormatDouble(GetMaxX(), pwc, MAX_DOUBLE_SIZE);
    xmlWriter->WriteCharacters(pwc);
    xmlWriter->WriteEndElement();

    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsBoundingBoxMaxY);
    FdoCommonStringUtil::FormatDouble(GetMaxY(), pwc, MAX_DOUBLE_SIZE);
    xmlWriter->WriteCharacters(pwc);
    xmlWriter->WriteEndElement();

    xmlWriter->WriteEndElement();
}

void FdoWcsOvBoundingBoxDefinition::Dispose(void)
{
    delete this;
}

FdoStringP FdoWcsOvBoundingBoxDefinition::GetQualifiedName()
{
    FdoStringP ret = this->GetName ();
    FdoPtr<FdoPhysicalElementMapping> parent = this->GetParent ();
    if (parent != NULL)
    {
        FdoStringP parentQName = parent->GetQualifiedName ();
        if (parentQName.GetLength () > 0)
        {
            ret = parentQName + L"." + ret;
        }
    }

    return ret;
}
